package persistence.Impl;

import domain.Diary;
import persistence.DiaryDAO;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: petStore
 * @package: persistence.Impl
 * @className: DiaryDAOImpl
 * @author: suqianbei
 * @description: TODO
 * @date: 2021/11/3 15:21
 * @version: 1.0
 */
public class DiaryDAOImpl implements DiaryDAO {
    private static final String INSERT_NEW_DIARY="INSERT INTO DIARY(NAME,date,action) VALUES(?,?,?)";
    private static final String SELECT_ALL_DIARY="SELECT NAME,DATE,ACTION FROM DIARY";
    private static final String SELECT_DIARY_BY_NAME="SELECT NAME,DATE,ACTION FROM DIARY WHERE NAME=?";
    private static final String SELECT_DIARY_BY_KEYWORD="SELECT NAME,DATE,ACTION FROM DIARY WHERE name=? and LOWER(ACTION) like ? ";

    @Override
    public List<Diary> getAllDiary() {
        List<Diary> diaryList=new ArrayList<>();
        try {
            Connection connection=DBUtil.getConnection();
            Statement statement=connection.createStatement();
            ResultSet resultSet=statement.executeQuery(SELECT_ALL_DIARY);
            while (resultSet.next())
            {
                Diary diary=new Diary();
                diary.setName(resultSet.getString(1));
                diary.setDate(resultSet.getString(2));
                diary.setAction(resultSet.getString(3));
                diaryList.add(diary);
            }
            DBUtil.close(connection,statement,resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return diaryList;
    }

    @Override
    public List<Diary> getDiaryByName(String name) {
        List<Diary> diaryList=new ArrayList<>();
        try {
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(SELECT_DIARY_BY_NAME);
            preparedStatement.setString(1,name);
            ResultSet resultSet=preparedStatement.executeQuery();
            while (resultSet.next())
            {
                Diary diary=new Diary();
                diary.setName(resultSet.getString(1));
                diary.setDate(resultSet.getString(2));
                diary.setAction(resultSet.getString(3));
                diaryList.add(diary);
            }
            DBUtil.close(connection,preparedStatement,resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return diaryList;
    }

    @Override
    public List<Diary> getDiaryByKeyword(String name,String keyword) {
        List<Diary> diaryList=new ArrayList<>();
        try {
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(SELECT_DIARY_BY_KEYWORD);
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,keyword);
            ResultSet resultSet=preparedStatement.executeQuery();
            while (resultSet.next())
            {
                Diary diary=new Diary();
                diary.setName(resultSet.getString(1));
                diary.setDate(resultSet.getString(2));
                diary.setAction(resultSet.getString(3));
                diaryList.add(diary);
            }
            DBUtil.close(connection,preparedStatement,resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return diaryList;
    }
    @Override
    public void insertNewDiary(Diary diary) {
        try {
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(INSERT_NEW_DIARY);
            preparedStatement.setString(1,diary.getName());
            preparedStatement.setString(2,diary.getDate());
            preparedStatement.setString(3,diary.getAction());
            preparedStatement.executeUpdate();
            DBUtil.close(connection,preparedStatement,null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
